﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gift.WebApp;
using Gift.Model;
using System.Text;

namespace WebSite.admin.company
{
    public partial class companyList : BaseAdmin
    {

    

      public  string CompanyParamId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    return Request.QueryString["id"];
                }
                else
                {
                    return "0";
                }
            }
        }

        public string HeaderHtml
        {
              get
            {
                if (! string.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"] != "0")
                {

                    tbCompany oCompany = BaseFactory.theCompany.GetModel(int.Parse(Request.QueryString["id"]));

                    string sHead = string.Format(" <div  class=\"navgater\"><img src=\"../images/module.gif\" width=\"20\" height=\"22\" alt=\"\"/> "
                                              +" 公司名称：{0} "
                                              +" <a style=\" display:block; float:right;\" href=\"javascript:location.href='companyList.aspx?id={1}';\" ><img src=\"../images/history.gif\" border=\"0\" /></a>"
                                              +" </div>"
                                              ,  oCompany.Name
                                              ,  oCompany.ParentId.ToString()
                                              );
                    return sHead;
                }
                  return string.Empty;
   
            }
 
            
        }

        bool _isEdit = false;
        bool _isDeleted = false;
        bool _isUser = false;
        string _addDisplay = "none";

        public string AddDisplay
        {
            get { return _addDisplay; }
        }

        public void Page_Load(object sender, EventArgs e)
        {
            this.ValidateLoginUser();
            if (!IsPostBack)
            {
                this.BindProvenctList();
                this.BindCompanyLevel();
                this.BindGridView();
            }
        }

        void ValidateLoginUser()
        {
            
                if (!base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyList))
                {
                    base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                    base.Response.End();
                }
                if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyAdd))
                {
                    _addDisplay = "block";
                    _isUser = true;
                }
                if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyEdit))
                {
                    _isEdit = true;
                }
                if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyDeleted))
                {
                    _isDeleted = true;
                }

        }

        void BindGridView()
        {
            DataTable dt;
            StringBuilder sbWhere = new StringBuilder();
            sbWhere.AppendFormat(" (id={0} or parentId = {0})",base.Administrator.CompanyId);


            if (this.txtKeywords.Text.Trim() != "")
            {
                sbWhere.AppendFormat(" and (Name like '%{0}%' or CompanyCode = '{0}' or CompanyShortName like '%{0}%')", this.txtKeywords.Text.Trim());
            }
            if (this.ddlLevleResult.SelectedItem.Value != "")
            {
                sbWhere.AppendFormat(" and LevelId = {0} ", this.ddlLevleResult.SelectedItem.Value);
            }
            if (this.ddlProvence.SelectedItem.Value != "")
            {
                sbWhere.AppendFormat( " and Provence = '{0}" , this.ddlProvence.SelectedItem.Value);
            }
            dt = BaseFactory.theCompany.GetList(sbWhere.ToString()).Tables[0];//.GetList("", "");


            if (dt != null)
            {
                this.GridView1.DataSource = dt;
                this.GridView1.DataBind();

                this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
                this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
                this.LblRecordCount.Text = "总计 " + dt.DefaultView.Count.ToString() + "个记录";
                if (dt.Rows.Count == 0)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                    this.LblCurrentIndex.Visible = false;
                    this.LblPageCount.Visible = false;
                    this.LblRecordCount.Visible = false;
                }
                else if (this.GridView1.PageCount == 1)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                }
                this.btnFirst.CommandName = "1";
                this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
                this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
                this.btnLast.CommandName = this.GridView1.PageCount.ToString();
                this.ddlCurrentPage.Items.Clear();
                if (this.GridView1.PageCount > 0)
                {
                    for (int i = 1; i <= this.GridView1.PageCount; i++)
                    {
                        this.ddlCurrentPage.Items.Add(i.ToString());
                    }
                    this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
                }
            }
        }


        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }



        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string sIds = "";
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    sIds = sIds + this.GridView1.DataKeys[i].Value + ",";
                }
            }
            if (sIds.Length > 0)
            {
                sIds = sIds.Remove(sIds.Length - 1);

                BaseFactory.theCompany.DeleteMany(sIds);
                base.Response.Write("<script>alert('批量删除成功!')</script>");
                this.BindGridView();
            }

        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.BindGridView();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.BindGridView();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                if (box != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        //当鼠标停留时更改背景色
                        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D7EDFD'");
                        //当鼠标移开时还原背景色
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                        base.SetGridListPerssion(e, _isEdit, _isDeleted, _isUser);
                        string sTempCompanyId = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                       if (sTempCompanyId == base.Administrator.CompanyId)
                        {
                            e.Row.FindControl("divEdit").Visible = false;
                            e.Row.FindControl("divDeleted").Visible = false;
                            e.Row.FindControl("divUser").Visible = false;
                        }

                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    }
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGridView();
        }

    

        void BindCompanyLevel()
        {
            DataSet ds = BaseFactory.theCompany.GetCompanyLevelList(base.Administrator.CompanyId);

            this.ddlLevleResult.DataTextField = "LevelName";
            this.ddlLevleResult.DataValueField = "Id";
            this.ddlLevleResult.DataSource = ds.Tables[0].DefaultView;
            this.ddlLevleResult.DataBind();
            this.ddlLevleResult.Items.Insert(0, new ListItem("请选择商家级别...", ""));
        }


        void BindProvenctList()
        {
            DataSet ds = BaseFactory.theCompany.GetProvenceList(base.Administrator.CompanyId);
            this.ddlProvence.DataTextField = "provence";
            this.ddlProvence.DataValueField = "provence";
            this.ddlProvence.DataSource = ds;
            this.ddlProvence.DataBind();
            this.ddlProvence.Items.Insert(0, new ListItem("请选择省份", ""));
        }
    }
}